local skel = fk.CreateSkill {
  name = "lb_zero__xiajue",
  max_branches_use_time = function(self, player)
    local list = {}
    local room = Fk:currentRoom()
    for _, to in ipairs(room.players) do
      if to ~= player then
        list[tostring(to.id)] = { [Player.HistoryRound] = 1 }
      end
    end
    return list
  end,
}

Fk:loadTranslationTable {
  ["lb_zero__xiajue"] = "黠谲",
  [":lb_zero__xiajue"] = "每轮限两次，你可以获得一名本轮未成为过〖黠谲〗目标的其他角色一张牌，以视为使用一张基本牌，若未造成伤害，你须交给其两张牌。",

  ["#lb_zero__xiajue"] = "黠谲：你可以获得一名本轮未成为过〖黠谲〗目标的其他角色一张牌，以视为使用一张基本牌，若未造成伤害，你须交给其两张牌",
  ["#lb_zero__xiajue-choose"] = "黠谲：请选择一名本轮未成为过〖黠谲〗目标的其他角色，获得其一张牌",
  ["#lb_zero__xiajue-obtain"] = "黠谲：请获得 %src 一张牌",
  ["#lb_zero__xiajue-give"] = "黠谲：请交给 %src 两张牌",

  ["@[skill_players]lb_zero__xiajue"] = "黠谲",

  ["$lb_zero__xiajue1"] = "狡兔出手，万事无忧。",
  ["$lb_zero__xiajue2"] = "明智的选择。",
  ["$lb_zero__xiajue3"] = "有事？我可是很贵的。",
}

skel:addEffect("viewas", {
  anim_type = "control",
  pattern = ".|.|.|.|.|basic",
  prompt = "#lb_zero__xiajue",
  interaction = function(self, player)
    local all_choices = Fk:getAllCardNames("b")
    local choices = player:getViewAsCardNames(skel.name, all_choices)
    if #choices > 0 then
      return UI.CardNameBox { choices = choices, all_choices = all_choices }
    end
  end,
  card_filter = Util.FalseFunc,
  view_as = function(self, player, cards)
    if not self.interaction.data then return nil end
    local card = Fk:cloneCard(self.interaction.data)
    card.skillName = skel.name
    return card
  end,
  before_use = function(self, player, use)
    local room = player.room
    local tos = table.filter(room:getOtherPlayers(player), function(to)
      return skel:withinBranchTimesLimit(player, tostring(to.id), Player.HistoryRound) and not to:isNude()
    end)
    if #tos > 0 then
      local to = room:askToChoosePlayers(player, {
        targets = tos,
        min_num = 1,
        max_num = 1,
        skill_name = skel.name,
        prompt = "#lb_zero__xiajue-choose",
        cancelable = false,
      })[1]
      player:addSkillBranchUseHistory(skel.name, tostring(to.id))
      local card = room:askToChooseCard(player, {
        target = to,
        flag = "he",
        skill_name = skel.name,
        prompt = "#lb_zero__xiajue-obtain:" .. to.id,
      })
      room:obtainCard(player, card, false, fk.ReasonPrey, player, skel.name)
      use.extra_data = use.extra_data or {}
      use.extra_data.lb_zero__xiajue = to.id
      return
    end
    return skel.name
  end,
  after_use = function (self, player, use)
    if player:isNude() or use.damageDealt or not (use.extra_data and use.extra_data.lb_zero__xiajue) then
      return
    end
    local room = player.room
    local to = room:getPlayerById(use.extra_data.lb_zero__xiajue)
    if to:isAlive() then
      local cards = player:getCardIds("he")
      if #cards > 2 then
        cards = room:askToCards(player, {
          min_num = 2,
          max_num = 2,
          skill_name = skel.name,
          include_equip = true,
          cancelable = false,
          prompt = "#lb_zero__xiajue-give:" .. to.id
        })
      end
      room:obtainCard(to, cards, false, fk.ReasonGive, player, skel.name)
    end
  end,
  enabled_at_play = function(self, player)
    return player:usedSkillTimes(skel.name, Player.HistoryRound) < 2
        and table.find(Fk:currentRoom().alive_players, function(to)
          return to ~= player and skel:withinBranchTimesLimit(player, tostring(to.id), Player.HistoryRound) and
          not to:isNude()
        end)
  end,
  enabled_at_response = function(self, player, response)
    return not response and player:usedSkillTimes(skel.name, Player.HistoryRound) < 2
        and table.find(Fk:currentRoom().alive_players, function(to)
          return to ~= player and skel:withinBranchTimesLimit(player, tostring(to.id), Player.HistoryRound) and
          not to:isNude()
        end)
  end,
})

skel:addAcquireEffect(function(self, player, is_start)
  player.room:setPlayerMark(player, "@[skill_players]lb_zero__xiajue",
    { name = skel.name, scope = Player.HistoryRound, players = table.map(player.room.players, Util.IdMapper) })
end)

skel:addLoseEffect(function(self, player, is_death)
  player.room:setPlayerMark(player, "@[skill_players]lb_zero__xiajue", 0)
end)

return skel
